<template>
  <div class="new-comment">
    <div class="comment">
      <send-message ref="message" @sendCommentData="sendCommentData" />
    </div>
    <div class="comment-list">
      <comment-list ref="list" :label="thisData.name" @sendCommentNumber="sendCommentNumber" />
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from "Vuex";
import commentList from "@/components/newComment/commentList.vue";
import sendMessage from "@/components/newComment/sendMessage.vue";
export default {
  name: "new-comment",
  data() {
    return {
      id: ""
    };
  },
  props: {
    thisData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  components: {
    "send-message": sendMessage,
    "comment-list": commentList
  },
  mounted() {
    this.id = this.$route.params.id;
  },
  computed: {
    ...mapState("user", ["userInfoData"])
  },
  methods: {
    sendCommentNumber(number) {
      this.$emit("_sendCommentNumber", number);
    },
    sendCommentData(data) {
      let collections = {
        resourceId: this.id,
        resourceUserName: this.thisData.userName,
        resourceCommentData: data,
        sendUserName: this.userInfoData.userName,
        sendUserCoverUrl: this.userInfoData.coverUrl || ""
      };
      this.setResourceCommentData(collections)
        .then(res => {
          if (res.data.ok === 1) {
            this.$refs.message.data = "";

            this.$refs.list._sendMessageToUser(data, this.thisData.userName);
            this.$refs.list.initMethods();
            alert("回复成功！");
          } else {
            console.log(res);
          }
        })
        .catch(err => {
          console.log("错误提示" + err);
        });
    },
    ...mapActions("user", ["setResourceCommentData"])
  }
};
</script>

<style lang="sass" scoped>
@import "@/assets/default.sass"
</style>